<!-- Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!--
    Uses the HTML <table> content model, but automatically adds a number to
    the <caption>.  Use this as <table is="cxx-table">, unlike the other custom
    elements.

    With class="list", the table represents a list, which will be organized
    into the number of columns set by the [columns] attribute.
-->
<polymer-element name="cxx-table" extends="table" constructor="CxxTableElement" attributes="columns">
  <template>
    <style>
      :host { margin-left: auto; margin-right: auto;
              border-collapse: collapse;
              border: thin solid black; }
      caption { white-space: nowrap; }
      polyfill-next-selector { content: 'caption caption' }
      ::content caption { display: inline; }

      polyfill-next-selector { content: 'th, td' }
      ::content th, ::content td {
        border-style: solid none; border-color: black;
        border-width: thin;
        padding: 0 0.25em; }

      polyfill-next-selector { content: ':host(.column-rules) th, :host(.column-rules) td' }
      :host(.column-rules) ::content th,
      :host(.column-rules) ::content td {
          border-left-style: solid;  border-right-style: solid;}

      polyfill-next-selector { content: 'th' }
      ::content th { border-bottom: double medium; }
      polyfill-next-selector { content: ':host(.single-border) th' }
      :host(.single-border) ::content th { border-bottom: solid thin; }

      polyfill-next-selector { content: ':host(.center) td' }
      :host(.center) ::content td { text-align: center; }

      polyfill-next-selector { content: ':host(.list) td' }
      :host(.list) ::content td { border: none; vertical-align: top; }

      polyfill-next-selector { content: ':host(.list) ul' }
      :host(.list) ::content ul { padding-left: 0; margin: 0;}
      polyfill-next-selector { content: ':host(.list) ul li::before' }
      :host(.list) ::content ul li::before { content: '';}
    </style>

    <caption>Table {{table_num}} — <wbr><content select="caption"></content></caption>
    <content></content>
  </template>
  <script>
    (function() {
      function renumberTables() {
        document.querySelectorAll('[is=cxx-table]').array().forEach(function(table, index) {
          table.table_num = index + 1;
        });
      }
      Polymer('cxx-table', {
        arrangeColumns: function() {
          // Balance list-tables so we don't have to do it manually.  I tried
          // using CSS columns, but the appearance isn't as nice.
          if (this.classList.contains('list')) {
            // Set ARIA role so screenreaders go straight to the list.
            this.setAttribute('role', 'presentation');

            // Avoid messing with tables that are manually formatted.
            if (this.rows.length == 1 &&
                this.rows[0].cells.length == 1 &&
                this.rows[0].cells[0].firstElementChild instanceof HTMLUListElement) {
              var tr = this.rows[0];
              var elems = tr.querySelectorAll('li').array();
              tr.cells[0].remove();
              var rows = Math.ceil(elems.length / this.columns);
              for (var column = 0; column < this.columns; column++) {
                var td = document.createElement('td');
                tr.appendChild(td);
                var ul = document.createElement('ul');
                td.appendChild(ul);
                for (var row = 0; row < rows; row++) {
                  var index = column*rows + row;
                  if (index < elems.length)
                    ul.appendChild(elems[index]);
                }
              }
            }
          }
        },
        domReady: function() {
          renumberTables.apply(this);
          this.arrangeColumns();
        },
        detached: renumberTables,
      });
    })();
  </script>
</polymer-element>
